package cn.itcast.order.service;

import cn.itcast.order.FeiginClient.UserClient;
import cn.itcast.order.mapper.OrderMapper;
import cn.itcast.order.pojo.Order;
import cn.itcast.order.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    UserClient userClient;

    public Order queryOrderById(Long orderId) {
        // 1.查询订单
        Order order = orderMapper.findById(orderId);

//        //2.远程访问根据用户id查询用户信息的微服务,获取数据
////        String url="http://localhost:8081/user/"+order.getUserId();
//        String url="http://userservice/user/"+order.getUserId();
//        User user=restTemplate.getForObject(url/*访问远程微服务的url*/,User.class/*指定访问返回的数据的类型，不指定则默认为json*/);

        //2.利用Feign发起http请求，查询用户
        User user= userClient.findById(order.getUserId());


        //3.封装user信息
        order.setUser(user);
        // 4.返回
        return order;
    }
}
